/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.saville.msgcomp;

import com.saville.serdes.*;
import java.util.concurrent.atomic.*;

public class McId implements ISerdes<McId> {
    public static final int SERDES_ID = 3;
    public static final String SERDES_ID_STRING = "3";

    public transient IMc handler;
    
    public String name;
    public String ipAddr;
    public int ipPort;
    public int id;
    public int remoteId;
    public boolean remote;

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();

        s.append(" name=");
        s.append(name);
        s.append(" ipAddr=");
        s.append(ipAddr);
        s.append(" ipPort=");
        s.append(ipPort);
        s.append(" id=");
        s.append(id);
        s.append(" remoteId=");
        s.append(remoteId);
        s.append(" remote=");
        s.append(remote);

        return s.toString();
    }

    public String serdesIdString() {
        return SERDES_ID_STRING;
    }

    @Override
    public int serdesId() {
        return SERDES_ID;
    }

    @Override
    public ISerdes<McId> make() {
        return new McId();
    }

    @Override
    public void serialize(SerdesBinary serdes) {
        serdes.writeInt(serdesId());
        serdes.writeString(name);
        serdes.writeString(ipAddr);
        serdes.writeInt(ipPort);
        serdes.writeInt(id);
        serdes.writeInt(remoteId);
        serdes.writeBoolean(remote);
    }

    @Override
    public McId deserialize(SerdesBinary serdes) {
        name = serdes.readString();
        ipAddr = serdes.readString();
        ipPort = serdes.readInt();
        id = serdes.readInt();
        remoteId = serdes.readInt();
        remote = serdes.readBoolean();

        return this;
    }

    @Override
    public void serialize(SerdesJson serdes) {
        try {
            serdes.startObject();
            serdes.writeKey(serdesIdString());
            serdes.startArray();
            serdes.writeString(name);
            serdes.writeString(ipAddr);
            serdes.writeInt(ipPort);
            serdes.writeInt(id);
            serdes.writeInt(remoteId);
            serdes.writeBoolean(remote);
            serdes.endArray();
            serdes.endObject();
        } catch (Exception e) {
            throw new RuntimeException("McId#serialize json exception");
        }
    }

    @Override
    public McId deserialize(SerdesJson serdes) {
        try {
            char c = serdes.nextChar();
            if (c != '[') {
                throw new RuntimeException("McId#deserialize json expecting '['");
            }
            name = serdes.readString();
            ipAddr = serdes.readString();
            ipPort = serdes.readInt();
            id = serdes.readInt();
            remoteId = serdes.readInt();
            remote = serdes.readBoolean();
            c = serdes.nextChar();
            if (c != ']') {
                throw new RuntimeException("McId#deserialize json expecting ']'");
            }
        } catch (Exception e) {
            // FXIME: Best way to handle nested throws so as not to hide?
            throw new RuntimeException("McId#deserialize json exception");
        }
        return this;
    }

    /*
     * This code should be present in all classes that implement ISerdes
     */
    {
        if (registered.compareAndSet(0, 1)) {
            SerdesBinary.register(serdesId(), this);
            SerdesJson.register(serdesId(), this);
        }
    }

    private static final AtomicInteger registered = new AtomicInteger();
}
